<template>
  <div>
    <el-card class="box-card">
      <el-button
        type="primary"
        size="default"
        icon="Plus"
        @click="addTrademark"
        v-has="`btn.Trademark.add`"
        v-if="userStore.buttons.includes('btn.Category.add')"
      >
        添加品牌
      </el-button>
      <!-- 表格组件 -->
      <el-table :data="trademarkArr" border style="margin: 10px 0" height="720px">
        <el-table-column type="index" label="序号" width="80px" align="center"></el-table-column>
        <el-table-column prop="tmName" label="品牌名称" align="center"></el-table-column>
        <el-table-column label="品牌LOGO" align="center">
          <template #="{ row, $index }">
            <img :src="row.logoUrl" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column label="品牌操作" width="160px" align="center">
          <template #="{ row, $index }">
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              @click="updateTrademark(row)"
              v-if="userStore.buttons.includes('btn.Trademark.update')"
            ></el-button>
            <el-popconfirm
              :title="`您确定要删除${row.tmName}?`"
              width="250px"
              icon="Delete"
              @confirm="removeTradeMark(row.id)"
              v-if="userStore.buttons.includes('btn.Trademark.remove')"
            >
              <template #reference>
                <el-button type="primary" size="small" icon="Delete"></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :small="true"
        :background="true"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <AddDialog ref="addDialogRef" :info="activeRow" @update="getHasTrademark" />
  </div>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus';
import { ref, onMounted } from 'vue';
import { reqHasTrademark, reqDeleteTrademark } from '@/api/product/trademark';
import type { Records } from '@/api/product/trademark/type';
import AddDialog from './component/addDialog.vue';
import useUserStore from '@/store/modules/user';

const userStore = useUserStore();

let currentPage = ref<number>(1); // 当前页码
let pageSize = ref<number>(10); // 每页显示多少条数据
let pageSizes = ref([10, 20, 30]);
let total = ref<number>(0); // 总共条数
let addDialogRef = ref<InstanceType<typeof AddDialog>>();

let trademarkArr = ref<Records>([]);
let activeRow = ref({});

const handleSizeChange = (val: any) => {
  pageSize.value = val;
  getHasTrademark();
};
const handleCurrentChange = (current: any) => {
  currentPage.value = current;
  getHasTrademark();
};

onMounted(() => {
  getHasTrademark();
});

const getHasTrademark = async () => {
  let res = await reqHasTrademark(currentPage.value, pageSize.value);
  if (res.code == 200) {
    trademarkArr.value = res.data.records;
    total.value = res.data.total;
  } else {
  }
};

const addTrademark = () => {
  activeRow.value = {};
  addDialogRef.value?.open();
};

const updateTrademark = (row: any) => {
  activeRow.value = row;
  addDialogRef.value?.open();
};

const removeTradeMark = async (id: any) => {
  //点击确定按钮删除已有品牌请求
  let result = await reqDeleteTrademark(id);
  if (result.code == 200) {
    //删除成功提示信息
    ElMessage({
      type: 'success',
      message: '删除品牌成功',
    });
    //再次获取已有的品牌数据
    getHasTrademark();
  } else {
    ElMessage({
      type: 'error',
      message: '删除品牌失败',
    });
  }
};
</script>

<style lang="scss" scoped></style>
